package com.yao.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yao.common.Result;
import com.yao.dto.GoodsList;
import com.yao.po.Flower;
import com.yao.po.Flowers;
import com.yao.po.FlowersImages;
import com.yao.service.IFlowersImagesService;
import com.yao.service.IFlowersService;
import com.yao.utils.BeanUtils;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    private final IFlowersService flowersService;
    private static final Logger log = LoggerFactory.getLogger(GoodsController.class);
    private final IFlowersImagesService flowersImagesService;

    public GoodsController(IFlowersService flowersService, IFlowersImagesService flowersImagesService) {
        this.flowersService = flowersService;
        this.flowersImagesService = flowersImagesService;
    }

    /**
     * 获取商品列表
     * @param current 当前页
     * @param size 每页大小
     * @param category1Id  分类1ID
     * @param category2Id  分类2ID
     * @return 商品列表
     */
    @GetMapping("/list/{page}/{limit}")
    public Result<GoodsList> getGoodsList(@PathVariable("page") Long current,
                                          @PathVariable("limit") Long size,
                                          Integer category1Id,
                                          Integer category2Id) {
        log.info("获取商品列表...");
        IPage<Flowers> flowersPage = new Page<>(current, size);
        GoodsList goodsList1 = new GoodsList();
        LambdaQueryWrapper<Flowers> queryWrapper = new LambdaQueryWrapper<>();
        if (category1Id != null) {
            queryWrapper.eq(Flowers::getCategory1Id, category1Id);
        }
        if (category2Id != null) {
            queryWrapper.eq(Flowers::getCategory2Id, category2Id);
        }
        IPage<Flowers> resultPage = flowersService.page(flowersPage, queryWrapper);
        List<Flowers> records = resultPage.getRecords();
        goodsList1.setRecords(records);
        long total = resultPage.getTotal();
        goodsList1.setTotal(total);

        return Result.success(goodsList1);
    }

    /**
     * 获取商品详情
     * @param id 商品ID
     * @return 商品详情
     */
    @GetMapping("/{goodsId}")
    public Result<Flower> getGoodsById(@PathVariable("goodsId") Long id) {
        log.info("获取商品详情...");
        Flowers flowers = flowersService.getById(id);
        List<String> lis = new ArrayList<>();
        Flower flower = BeanUtils.copyBean(flowers, Flower.class);
        flowersImagesService.lambdaQuery().eq(FlowersImages::getFlowersId, flowers.getFloralLanguage()).list()
                .forEach(ll -> lis.add(ll.getImageUrl()));
        flower.setFloralLanguage(lis.stream().toList());
        return Result.success(flower);
    }
}
